﻿
using Furion.FriendlyException;
using System.Net.Http.Headers;
using System.Text;

/// <summary>
/// 网络
/// </summary>
public class Http
{

    /// <summary>
    /// get
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="url"></param>
    /// <param name="header"></param>
    /// <returns></returns>
    public static async Task<T> Get<T>(string url, Dictionary<string, string> header = null)
    {
        using HttpClient client = new HttpClient();

        if (header != null) foreach (var item in header) client.DefaultRequestHeaders.Add(item.Key, item.Value);

        var response = await client.GetAsync(url);

        string json = await response.Content.ReadAsStringAsync();

        return json.ToObject<T>();

    }

    /// <summary>
    /// post请求 json格式
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="url"></param>
    /// <param name="data"></param>
    /// <param name="header"></param>
    /// <returns></returns>
    public static async Task<T> Post<T>(string url, object data, Dictionary<string, string> header = null)
    {
        using HttpClient client = new HttpClient();

        string paramJson = data.ToJson();

        using HttpContent content = new StringContent(paramJson, new MediaTypeHeaderValue("application/json"));

        if (header != null)
        {
            foreach (var item in header)
            {
                client.DefaultRequestHeaders.Add(item.Key, item.Value);
            }
        }

        var response = await client.PostAsync(url, content);

        Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

        string json = await response.Content.ReadAsStringAsync();

        return json.ToObject<T>();

    }

    
    /// <summary>
    /// post请求 返回文件流
    /// </summary>
    /// <param name="url"></param>
    /// <param name="data"></param>
    /// <param name="header"></param>
    /// <returns></returns>
    public static async Task<byte[]> Post(string url, object data, Dictionary<string, string> header = null)
    {
        using HttpClient client = new HttpClient();

        string paramJson = data.ToJson();

        using HttpContent content = new StringContent(paramJson, new MediaTypeHeaderValue("application/json"));

        if (header != null) foreach (var item in header) client.DefaultRequestHeaders.Add(item.Key, item.Value);

        var response = await client.PostAsync(url, content);

        if (response.StatusCode != System.Net.HttpStatusCode.OK) throw Oops.Oh($"请求失败{response.StatusCode}");

        return await response.Content.ReadAsByteArrayAsync();


    }

    /// <summary>
    /// 获取base64文件
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    public static async Task<string> GetFileBase64(string url)
    {
        using HttpClient client = new HttpClient();

        var response = await client.GetAsync(url);

        string contentType = response.Content.Headers.ContentType?.MediaType ?? "";

        byte[] file = await response.Content.ReadAsByteArrayAsync();

        string base64File = Convert.ToBase64String(file);

        return ($"data:{contentType};base64,{base64File}");
    }

    /// <summary>
    /// 上传文件 只设置body
    /// </summary>
    /// <param name="url"></param>
    /// <param name="body"></param>
    /// <returns></returns>
    public static async Task<dynamic> UploadOnlyBody(string url, Dictionary<string, string> body)
    {
        using HttpClient client = new HttpClient();

        using var content = new MultipartFormDataContent();

        foreach (var item in body)
        {
            content.Add(new StringContent(item.Value), item.Key);
        }


        var result = await client.PostAsync(url, content);

        string json = await result.Content.ReadAsStringAsync();

        return json.ToObject<dynamic>();
    }

    /// <summary>
    /// 上传文件
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="url"></param>
    /// <param name="file"></param>
    /// <param name="header"></param>
    /// <returns></returns>
    public static async Task<T> Upload<T>(string url, byte[] file, Dictionary<string, string> header = null)
    {
        using var client = new HttpClient();

        using var content = new MultipartFormDataContent();

        using var byteContent = new ByteArrayContent(file);

        content.Add(byteContent, "file", "a.jpg");

        var result = await client.PostAsync(url, content);

        string json = await result.Content.ReadAsStringAsync();

        return json.ToObject<T>();
    }

    /// <summary>
    /// post
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="url"></param>
    /// <param name="data"></param>
    /// <param name="header"></param>
    /// <returns></returns>
    public static async Task<T> PostStr<T>(string url, string data, Dictionary<string, string> header = null)
    {
        using (HttpClient client = new HttpClient())
        {


            using (HttpContent content = new StringContent(data, Encoding.UTF8, "application/json"))
            {



                if (header != null)
                {
                    foreach (var item in header)
                    {
                        client.DefaultRequestHeaders.Add(item.Key, item.Value);
                    }
                }

                var response = await client.PostAsync(url, content);

                string json = await response.Content.ReadAsStringAsync();

                return json.ToObject<T>();
            }
        }
    }

    /// <summary>
    /// post表单提交
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="url"></param>
    /// <param name="data"></param>
    /// <returns></returns>
    public static async Task<T> PostForm<T>(string url, string data)
    {
        using (HttpClient client = new HttpClient())
        {
            using (HttpContent content = new StringContent(data, Encoding.UTF8, "application/x-www-form-urlencoded"))
            {
                var response = await client.PostAsync(url, content);

                string json = await response.Content.ReadAsStringAsync();

                return json.ToObject<T>();
            }
        }
    }

    /// <summary>
    /// post返回文件流
    /// </summary>
    /// <param name="url"></param>
    /// <param name="data"></param>
    /// <returns></returns>
    public static async Task<byte[]> Post(string url, object data)
    {
        using (HttpClient client = new HttpClient())
        {
            string paramsStr = data.ToJson();

            using (HttpContent content = new StringContent(paramsStr, Encoding.UTF8, "application/json"))
            {
                var response = await client.PostAsync(url, content);

                var header = response.Content.Headers.GetValues("content-type").First();

                if (header != null && header.Contains("json"))
                {
                    string json = await response.Content.ReadAsStringAsync();

                    var jObj = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(json);

                    throw Oops.Oh("errmsg");
                }

                return await response.Content.ReadAsByteArrayAsync();
            }
        }
    }
}

